package com.example.travel.Service;

import jakarta.servlet.http.HttpSession;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;

/**
 * 功能：
 * 作者：lln
 * 日期：2025/3/17 13:52
 */
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator{

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        // 获取HttpSession
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        // 将HttpSession存储到ServerEndpointConfig的配置中
        if (httpSession != null) {
            // 如果 HttpSession 不为 null，存储到 ServerEndpointConfig 的配置中
            sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
        } else {
            // 如果 HttpSession 为 null，记录日志或抛出自定义异常
            System.err.println("HttpSession is null in modifyHandshake. This may indicate a problem with session management.");
            // 可选：抛出自定义异常
            // throw new IllegalStateException("HttpSession is null in modifyHandshake");
        }
    }

}

